/**
 * 用作注册校验以及ajax服务端校验
 */
//非空校验 长度校验
function verificationUsername(){
    var data = $('#username').val();
    if(data.length === 0)
    {
        $('#registerErrorMsg1').text("用户名不能为空");
        return false;
    }
    else if(data.length <3 || data.length>12)
    {
        $('#registerErrorMsg1').text("长度需要在3-12位");
        return false;
    }
    return true;
}
//密码校验
var passwordVer1 = false;
var passwordVer2 = false;
function verificationPassword(){
    var data = $('#password').val();
    var again = $('#password1').val();
    if(data.length === 0)
    {
        $('#registerErrorMsg2').text("密码不能为空");
        return ;
    }
    else if(data.length <6 || data.length>20)
    {
        $('#registerErrorMsg2').text("长度需要在6-20位");
        return ;
    }
    passwordVer1 = true;
}
//ajax校验
function ServerVerification(){
    var data = $('#username').val();
    $.get("userExist","username="+data,function(result){
        if(result === "true"){
            $('#registerErrorMsg1').text("用户名已存在");
            return false;
        }
        else
        {
            $('#registerErrorMsg1').text("此用户名可以注册");
            return true;
        }
    });
}
//用户名校验
$(document).ready(function(){
    $('#username').blur(function(){
        if(verificationUsername())
            ServerVerification();
    });
});
$(document).ready(function(){
    $('#username').focus(function(){
        $('#registerErrorMsg1').text("");
    });
});
//密码校验
$(document).ready(function(){
    $('#password').blur(function(){
       verificationPassword();   
    });
});
$(document).ready(function(){
    $('#password').focus(function(){
       $('#registerErrorMsg2').text("");
    });
});
//两次输入密码校验
$(document).ready(function(){
    $('#password1').blur(function(){
    if($('#password1').val() !==  $('#password').val())
    {
        $('#registerErrorMsg2').text("密码输入不一致");
        return ;
    }else
    {
        $('#registerErrorMsg2').text("");
        passwordVer2 = true;
    }
    });
});

//提交时候需要验证
$(document).ready(function(){
   $("#registerform").submit(function(){
       verificationPassword();
       console.log("-----3------");
       if(verificationUsername() && passwordVer1 && passwordVer2)
       	{
            return true;
        }else
            return false;
   });
});
